<template>
  <div class="global_order marginTop10">
    <div class="main_white padding10 paddingTop0">
      <div class="main_ti">
        <div class="radio_box">
          <el-radio-group v-model="radio">
            <el-radio :label="0">默认</el-radio>
            <el-radio :label="1">指定合作商</el-radio>
            <el-radio :label="2">检测设备</el-radio>
            <el-radio :label="3">打印报告</el-radio>
            <!-- <el-radio :label="3">H5端登录</el-radio> -->
          </el-radio-group>
        </div>
        <div v-if="radio == 0">
          <el-form
            :model="drug_check_form_1"
            :inline="false"
            class="demo-form-inline"
            ref="drug_check_form_1"
            label-width="80px"
            :rules="rules"
          >
            <el-form-item class="flex_center" prop="name">
              <span slot="label"> 账号:</span>
              <el-input
                v-model="drug_check_form_1.name"
                placeholder="默认"
              ></el-input>
            </el-form-item>
            <el-form-item class="flex_center" prop="password">
              <span slot="label"> 密码:</span>
              <el-input
                v-model="drug_check_form_1.password"
                :placeholder="drug_check_form_1_placeholder"
                type="password"
              ></el-input>
            </el-form-item>
            <el-form-item class="flex_center" prop="">
              <span slot="label"></span>
              <el-button @click="jump"> 立即登录 </el-button>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="radio == 1">
          <el-form
            :model="drug_check_form_2"
            :inline="false"
            class="demo-form-inline"
            ref="drug_check_form_2"
            label-width="80px"
            :rules="rules"
          >
            <el-form-item class="flex_center" prop="name">
              <span slot="label"> 账号: </span>
              <el-input
                v-model="drug_check_form_2.name"
                placeholder="指定合作商"
              ></el-input>
            </el-form-item>
            <el-form-item class="flex_center" prop="password">
              <span slot="label"> 密码:</span>
              <el-input
                v-model="drug_check_form_2.password"
                placeholder="指定合作商"
                show-password
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="radio == 2">
          <el-form
            :model="drug_check_form_3"
            :inline="false"
            class="demo-form-inline"
            ref="drug_check_form_3"
            label-width="120px"
            :rules="rules"
          >
            <el-form-item class="flex_center" prop="jiance_shebei_sn">
              <span slot="label"> 检测设备编号: </span>
              <el-input
                v-model="drug_check_form_3.jiance_shebei_sn"
                placeholder="输入检测设备编号"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="radio == 3">
          <el-form
            :model="print_form"
            :inline="false"
            class="demo-form-inline"
            ref="drug_check_form_1"
            label-width="80px"
          >
            <el-form-item class="flex_center" prop="name">
              <span slot="label"> 公章显示:</span>
              <el-switch
                v-model="print_form.offical_seal_switch"
                active-color="#03A98E"
                inactive-color="#DCDFE6"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-form>
        </div>

        <div class="clearfix"></div>
        <div class="newConBox_p100 paddingBottom10" id="newConBox">
          <div class="newConBox_h32">
            <div>
              <el-button type="primary" @click="preservation" v-repeatClick
                >保存</el-button
              >
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get_drug_info, set_drug_info } from "@/api/global/globalSystem";
import { setting_info } from "@/api/global/globalSystem";
import { setting_company } from "@/api/currency";
import rules from "@/rules/global/globalSet.js";
import { apiUrl, objKeySort, Md5 , packApi } from "@/utils/public";
export default {
  props: {
    form: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      rules,
      drug_check_form_1: {
        name: "",
        password: "",
        type: 1,
      },
      drug_check_form_1_placeholder: "默认",
      drug_check_form_2: {
        name: "",
        password: "",
        type: 2,
      },
      drug_check_form_3: {
        jiance_shebei_sn: null,
      },
      print_form:{
        // 公章是否显示开关
        offical_seal_switch: 0,
      },
      url: "",
      radio: 0,

      wap_url: null
    };
  },
  watch: {
    form: {
      handler(newval, oldval) {
        this.radio = newval.drug_residue_automatic;
        this.print_form.offical_seal_switch = newval.offical_seal_switch;
      },
      deep: true,
      immediate: true,
    },
    radio: {
      handler(newVal) {
        if (newVal == 2) {
          this.getInfo();
        }
      },
      immediate: true,
    },
  },

  mounted() {
    this.get_drug_info();
  },
  methods: {
    getInfo() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      setting_info(data).then((res) => {
        if (res.errcode == 0) {
          // this.$message.success("修改成功");
          this.drug_check_form_3.jiance_shebei_sn =
            res.data.info.jiance_shebei_sn;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    preservation() {
      if (this.radio == 0) {
        this.$refs["drug_check_form_1"].validate((valid) => {
          if (valid) {
            this.set_drug_info(this.drug_check_form_1);
          } else {
            return;
          }
        });
      } else if (this.radio == 1) {
        this.$refs["drug_check_form_2"].validate((valid) => {
          if (valid) {
            this.set_drug_info(this.drug_check_form_2);
          } else {
            return;
          }
        });
      } else if (this.radio == 2) {
        this.$refs["drug_check_form_3"].validate((valid) => {
          if (valid) {
            this.set_drug_info2(this.drug_check_form_3);
          } else {
            return;
          }
        });
      }else if(this.radio == 3){
        this.set_print_form(this.print_form);
      }
    },
    // 保存打印报告修改
    set_print_form(params){
      let data = packApi(params);
      setting_company(data).then((res) => {
        if (res.errcode == 0) {
          this.$message.success("修改成功");
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 保存修改
    set_drug_info2(params) {
      let form = {
        jiance_shebei_sn: params.jiance_shebei_sn,
      };
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      setting_company(data).then((res) => {
        if (res.errcode == 0) {
          this.$message.success("修改成功");
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 保存修改
    set_drug_info(params) {
      let form = {
        name: params.name,
        password: params.password,
        type: params.type,
        drug_residue_automatic: this.radio,
      };
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      console.log(data);
      set_drug_info(data).then((res) => {
        if (res.errcode == 0) {
          this.$message.success("修改成功");
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    passwordChange(val) {
      console.log(val);
    },
    // 请求数据
    get_drug_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      get_drug_info(data).then((res) => {
        if (res.errcode == 0) {
          if (res.data.data.length > 0) {
            res.data.data.filter((e) => {
              if (e.type == 2) {
                this.drug_check_form_2 = e;
              } else {
                this.drug_check_form_1_placeholder = "密码已存在，输入直接修改";
                e.password = "";
                this.drug_check_form_1 = e;
              }
            });
          }
        }
        this.url = res.data.url;
        this.wap_url = res.data.wap_url;
      });
    },

    // 立即登录
    jump() {
      if (!this.drug_check_form_1.name) {
        this.$message.warning("请先注册账号");
        return;
      }
      let sr = location.protocol + "//" + this.url;
      window.open(sr);
    },

    jumpH5() {
      if (!this.drug_check_form_1.name) {
        this.$message.warning("请先注册账号");
        return;
      }
      let sr = location.protocol + "//" + this.wap_url;
      window.open(sr);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";

/deep/.el-input {
  width: 400px;
}
.radio_box {
  padding-left: 80px;
  padding-bottom: 18px;
}
</style>
